__author__ = 'zoulida'
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from datetime import datetime, date
from statsmodels.regression import linear_model
import statsmodels.api as sm
import pymysql
import threading
from queue import Queue
import math
import tushare as ts
import matplotlib
#import talib
import seaborn as sns
sns.set(style="darkgrid", palette="muted", color_codes=True)
from scipy import stats,integrate
#%matplotlib inline
sns.set(color_codes=True)
matplotlib.rcParams['axes.unicode_minus']=False
plt.rcParams['font.sans-serif'] = ['SimHei']         # 中文显示
plt.rcParams['axes.unicode_minus'] = False   # 用来正常显示负号
#读取数据
def dataread():
    ts.set_token('****')
    pro = ts.pro_api()
    df_base=pro.index_dailybasic(ts_code="000001.SH", fields='trade_date,pe_ttm')
    df_stock=pro.index_daily(ts_code="000001.SH", start_date='20120101', end_date='20201010', fields='close,trade_date')
    df_bond=df=pro.index_daily(ts_code="000012.SH",start_date='20120101', end_date='20201010' , fields='trade_date,close')
    return df_base,df_stock,df_bond
df,df_stock,df_bond=dataread()
class prepare:
    #计算单日收益率
    #def __init__(self,freq):
    #    self.freq=freq
    def ret_base(self):
        df_stock.index=pd.to_datetime(df_stock.trade_date)
        ret_stock=(df_stock.close-df_stock.close.shift(-1))/df_stock.close.shift(-1)
        df_bond.index=pd.to_datetime( df_bond.trade_date  )
        ret_bond=(df_bond.close-df_bond.close.shift(-1))/df_bond.close.shift(-1)
        return ret_stock,ret_bond
    #计算pe均值，标准差及滚动均值
    def data_fun(self,freq,df):
        df.index=pd.to_datetime(df.trade_date )
        df=df.sort_index()
        df_=pd.DataFrame(df.pe_ttm )
        df_pe_std=df.rolling(window=252).std()[df.index>"20120101"]
        df_pe_std=df_pe_std.pe_ttm
        df_=df_.sort_index()
        df_roll=df_.rolling(window=freq).mean()
        df_pe_mean=df.rolling(window=252).mean()[df.index>"20120101"]
        df_pe_mean=df_pe_mean.pe_ttm
        return df_pe_mean,df_pe_std,df_roll.pe_ttm
mean,std,mean_roll=prepare().data_fun(20,df)
ret_stock,ret_bond=prepare().ret_base()

#class test_fun():
#信号处理
def sig_fun():
    """
    例如：
    滚动pe_ttm的均值mean_roll<pe_ttm的过去252个交易日的历史方差  股债比例：8:2

    """
    sig_stock=pd.Series(0,index=df_stock.index,dtype=float)
    sig_bond= pd.Series(0,index=df_bond.index,dtype=float)
    for i in range(len(df_bond)):
        if mean_roll[i]<mean[i]-1.5*std[i]:
            sig_stock[i]=1
            sig_bond[i]=0
        elif mean_roll[i]<mean[i]-0.5*std[i]:
            sig_stock[i]=0.4
            sig_bond[i]=0.6
        elif mean_roll[i]<mean[i]+1.5*std[i]:
            sig_stock[i]=0.2
            sig_bond[i]=0.8
        else:
            sig_stock[i]=0
            sig_bond[i]=1
    return sig_bond,sig_stock
sig_bond,sig_stock=sig_fun()
def ret_port( ret_bond,ret_stock):
    ret=ret_bond*sig_bond+ret_stock*sig_stock
    ret=ret.sort_index().dropna()
    ret_stock=ret_stock.sort_index()
    ret_bond =ret_bond.sort_index()
    cum_bond=np.cumprod(1+ret_bond)
    cum_stock=np.cumprod(1+ret_stock)
    cum=np.cumprod(1+ret)
    return cum,cum_stock,cum_bond,ret
cum,cum_stock,cum_bond,ret=ret_port( ret_bond,ret_stock)

def plot_fun():
    plt.plot(cum_bond ,label="000012.SH",color='k',linestyle='-')
    plt.plot(cum_stock,label="000001.SH",color='b',linestyle='-')
    plt.plot(cum,label="组合策略",color='r',linestyle='-')
    plt.title("净值走势")
    plt.legend(loc="upper left")

def performance(port_ret):
    port_ret=port_ret.sort_index(ascending=True)
    first_date = port_ret.index[0]
    final_date = port_ret.index[-1]
    time_interval = (final_date - first_date).days * 250 / 365
    # calculate portfolio's indicator
    nv = (1 + port_ret).cumprod()
    arith_mean = port_ret.mean() * 250
    geom_mean = (1 + port_ret).prod() ** (250 / time_interval) - 1
    sd = port_ret.std() * np.sqrt(250)
    mdd = ((nv.cummax() - nv) / nv.cummax()).max()
    sharpe = (geom_mean - 0) / sd
    calmar = geom_mean / mdd
    result = pd.DataFrame({'算术平均收益': [arith_mean], '几何平均收益': [geom_mean], '波动率': [sd],
                           '最大回撤率': [mdd], '夏普比率': [sharpe], '卡尔曼比率': [calmar]})
    print(result)
    return result
if __name__=="__main__":
    print("组合、债券、股票分别如下：")
    performance(ret)
    performance(ret_bond)
    performance(ret_stock)
    plot_fun()
